package ycl.scheduled.common.utils;


import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import ycl.scheduled.common.base.entity.BaseEntity;
import ycl.scheduled.common.base.mapper.BaseDao;
import ycl.scheduled.common.base.service.BaseService;
import ycl.scheduled.common.base.service.impl.BaseServiceImpl;

import java.util.Collections;

/**
 * @author: YCL
 * @date: 2022/01/19 22:32:02
 */
public class CodeGeneratorUtil {

	private static final String templatesPrefix = "/templates/";
	private static final String property = System.getProperty("user.dir");

	public static void main(String[] args) {

		FastAutoGenerator.create("jdbc:mysql://localhost:3306/ycl_scheduled",
				"root",
				"0000")
				.globalConfig(builder -> {
					builder.author("ycl")               //作者
							.outputDir(property + "\\src\\main\\java")    //输出路径(写到java目录)
							.enableSwagger()           //开启swagger
							.commentDate("yyyy-MM-dd HH:mm:ss");
					//.fileOverride();            //开启覆盖之前生成的文件
				})
				.packageConfig(builder -> builder.parent("ycl.scheduled")//包名输出
						.entity("entity")//实体类位置
						.service("service")//逻辑层位置
						.serviceImpl("service.impl")//逻辑实现类位置
						.controller("controller")//控制器位置
						.mapper("mapper")//数据库层位置
						.xml("mapper")//xml位置
						//xml输出位置
						.pathInfo(Collections.singletonMap(OutputFile.mapperXml, property + "\\src\\main\\resources\\mapper")))
				.strategyConfig(builder -> builder
						//实体类策略
						.entityBuilder()
						.formatFileName("%s")
						.superClass(BaseEntity.class)
						.enableLombok()
						.enableChainModel()
						.logicDeleteColumnName("deleted")
						.enableTableFieldAnnotation()
						.enableActiveRecord()

						//控制器策略
						.controllerBuilder()
						.formatFileName("%sController")
						.enableRestStyle()

						//数据库策略
						.mapperBuilder()
						.enableBaseResultMap()  //生成通用的resultMap
						.superClass(BaseDao.class)
						.enableBaseColumnList()
						.formatMapperFileName("%sMapper")
						.enableMapperAnnotation()
						.formatXmlFileName("%sMapper")

						//逻辑层策略
						.serviceBuilder()
						.superServiceClass(BaseService.class)
						.formatServiceFileName("%sService")
						.superServiceImplClass(BaseServiceImpl.class)
						.formatServiceImplFileName("%sServiceImpl")
				)
				.templateConfig(builder -> builder.disable(TemplateType.ENTITY)
						.entity(templatesPrefix + "entity.java")
						.service(templatesPrefix + "service.java")
						.serviceImpl(templatesPrefix + "serviceImpl.java")
						.mapper(templatesPrefix + "mapper.java")
						.mapperXml(templatesPrefix + "mapper.xml")
						.controller(templatesPrefix + "controller.java"))
				.execute();
	}
}
